package com.sphere.freereader.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.sphere.freereader.utils.Helper;

/**
 * Created by songbangbang on 16-8-17.
 */
public class BatteryLevelView extends View {

    private int mBatteryLevelNumber;

    // 电量大矩形
    private int mWidth;
    private int mHeight;

    // 电量小矩形
    private int mSmallWidth;
    private int mSmallHeight;

    private int mColor;
    private int mDefaultColor;

    private Paint mPaint;
    private Context mContext;

    public BatteryLevelView(Context context) {
        super(context);
        init(context);
    }

    public BatteryLevelView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BatteryLevelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        mWidth = Helper.dp2px(context, 26);
        mHeight = Helper.dp2px(context, 12);
        mSmallWidth = Helper.dp2px(context, 2);
        mSmallHeight = Helper.dp2px(context, 5);
        mPaint = new Paint();
        mDefaultColor = Color.BLACK;
        mPaint.setColor(mColor != 0 ? mColor : mDefaultColor);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int mWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int mWidthSize = MeasureSpec.getSize(widthMeasureSpec);
        int mHeightMode = MeasureSpec.getMode(heightMeasureSpec);
        int mHeightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (mWidthMode == MeasureSpec.EXACTLY && mHeightMode == MeasureSpec.EXACTLY) {
            setMeasuredDimension(mWidthSize, mHeightSize);
        }

        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        canvas.drawRect(0, 0, mWidth, mHeight, mPaint);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(0, 3, mWidth*0.8f/*电量百分比*/, mHeight - 3 , mPaint);
        canvas.drawRect(mWidth, (mHeight - mSmallHeight) / 2,
                mWidth + mSmallWidth, (mHeight - mSmallHeight) / 2 + mSmallHeight, mPaint);
//        Rect rect = new Rect();
//        mPaint.getTextBounds("80", 0, "80".length(), rect);
//        int textWidth = rect.width();
//        int textHeight = rect.height();
//        mPaint.setTextSize(dp2px(mContext, 10));
//        mPaint.setStyle(Paint.Style.FILL);
//        canvas.drawText("80", mWidth/2 - textWidth/2 ,mHeight /2 + textHeight , mPaint);

    }

    public void setColor(int mColor) {
        this.mColor = mColor;
    }

    public void setBatteryLevelNumber(int mBatteryLevelNumber) {
        this.mBatteryLevelNumber = mBatteryLevelNumber;
    }
}
